package com.elong.spring.proxy;

import java.lang.reflect.Method;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

public class CglibProxy implements MethodInterceptor{

    private Enhancer enhancer = new Enhancer();
    
    
    public Object getProxy(Class clazz){
	enhancer.setSuperclass(clazz);
	//设置本类为回调函数
	enhancer.setCallback(this);
	return enhancer.create();
    }
    
    /**
     * 拦截方法
      * override intercept
      * <p>Title: intercept</p>
      * <p>Description: </p>
      * @param obj
      * @param method
      * @param args
      * @param proxy
      * @return
      * @throws Throwable
     */
    @Override
    public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
	PerformanceMonitor.begin(obj.getClass().getName() + "." + method.getName());
	Object result = proxy.invokeSuper(obj, args);
	
	PerformanceMonitor.end();
	return result;
    }

}
